local skel = fk.CreateSkill {
  name = "emo__rongshuang",
}

local getMark = function (player)
  local mark = player:getMark("emo__luoxue_effect")
  if type(mark) ~= "table" then
    return {"emo__luoxue_losehp", "emo__luoxue_draw", "emo__luoxue_recover"}
  end
  return mark
end

Fk:loadTranslationTable{
  ["emo__rongshuang"] = "融霜",
  [":emo__rongshuang"] = "每回合每项限一次，你使用牌指定唯一目标后，可令其执行“落雪”第X项，并重置该项（X为你本回合使用牌的类型数）。",
  -- “每回合每项限一次”，本意是指：序号1 2 3各最多执行一次。
  ["@emo__rongshuang-turn"] = "融霜",
  ["#emo__rongshuang-ask"] = "融霜：你可以令 %src 执行“落雪”第 %arg 项(%arg2)",

  ["$emo__rongshuang1"] = "身居寒彻之所在，心慕莺飞之所往。",
  ["$emo__rongshuang2"] = "冬日寄暖阳，七分寒气曳锦霜。",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.firstTarget then
      if data.to and not data.to.dead then
        local num = #player:getTableMark("@emo__rongshuang-turn")
        return num > 0 and num < 4 and not table.contains(player:getTableMark("emo__rongshuang-turn"), num)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local mark = getMark(player)
    local num = #player:getTableMark("@emo__rongshuang-turn")
    if player.room:askToSkillInvoke(player, {
        skill_name = skel.name,
        prompt = "#emo__rongshuang-ask:"..data.to.id.."::"..num..":" .. mark[num],
      }) then
      event:setCostData(self, {tos = {data.to} })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    if not to or to.dead then return end
    local num = #player:getTableMark("@emo__rongshuang-turn")
    room:addTableMark(player, "emo__rongshuang-turn", num) -- 记录发动项序号
    local mark = getMark(player)
    local ch = mark[num]
    local orig = {"emo__luoxue_losehp", "emo__luoxue_draw", "emo__luoxue_recover"}
    mark[num] = orig[num]
    room:setPlayerMark(player, "emo__luoxue_effect", mark) -- 先修改，再执行，防插结
    if ch == "emo__luoxue_draw" then
      to:drawCards(3, skel.name)
    elseif ch == "emo__luoxue_losehp" then
      room:loseHp(to, 1, skel.name)
    elseif ch == "emo__luoxue_recover" then
      room:recover { num = 1, skillName = skel.name, who = to, recoverBy = player }
    else
      room:askToDiscard(to, {min_num = 1, max_num = 1, include_equip = true, skill_name = skel.name, cancelable = false})
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addTableMarkIfNeed(player, "@emo__rongshuang-turn", data.card:getTypeString().."_char")
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__rongshuang-turn", 0)
end)

skel:addAcquireEffect(function (self, player, is_start)
  player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
    local use = e.data
    if use.from == player then
      player.room:addTableMarkIfNeed(player, "@emo__rongshuang-turn", use.card:getTypeString().."_char")
    end
  end, Player.HistoryTurn)
end)

return skel
